package cn.b.sky.mes.pp.service.impl;

import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import cn.b.sky.mes.pp.dao.OperationTaskDao;
import cn.b.sky.mes.pp.enums.OperationTaskStatus;
import cn.b.sky.mes.pp.model.OperationTask;
import cn.b.sky.mes.pp.model.condition.OperationTaskCondition;
import cn.b.sky.mes.pp.service.OperationTaskConfirmService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import cn.b.sky.user.SecurityContextUtil;
import cn.b.sky.mes.cm.enums.NumberRuleEnums;
import cn.b.sky.user.service.NumberRuleService;
import cn.b.sky.mes.pp.service.OperationTaskService;
import cn.b.sky.mes.pp.service.ProductionControlService;

/**
 * 
 * <strong>Title : OperationTaskServiceImpl.java<br></strong>
 * <strong>Package : cn.b.sky.mes.pp.service.impl<br></strong>
 * <strong>Description : </strong>生产工序单接口实现类<br> 
 * <strong>Create on : 2016-8-2 下午1:55:12<br></strong>
 * <p>
 * @author JinQuan.Fu<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 * <strong>修改历史:</strong><br>
 * 修改人|修改日期|修改描述<br>
 * -------------------------------------------<br>
 * <br>
 * <br>
 */
@Service("operationTaskService")
public class OperationTaskServiceImpl implements OperationTaskService {

  @Resource
  OperationTaskDao operationTaskDao;

  @Resource
  OperationTaskConfirmService operationTaskConfirmService;

  @Resource
  ProductionControlService productionControlService;

  @Resource
  NumberRuleService numberRuleService;

  @Override
  public List<OperationTask> getOperationTaskByCondition(OperationTaskCondition condition) {
    if (condition == null) {
      return null;
    }
    return operationTaskDao.getOperationTaskByCondition(condition);
  }

  @Override
  public int getOperationTaskCountByCondition(OperationTaskCondition condition) {
    if (condition == null) {
      return 0;
    }
    return operationTaskDao.getOperationTaskCountByCondition(condition);
  }

  @Override
  public OperationTask getOperationTask(String siteCode, String operationTaskNum) {
    if (StringUtils.isBlank(operationTaskNum)) {
      return null;
    }
    return operationTaskDao.getoperationTask(siteCode, operationTaskNum);
  }

  @Override
  public String releaseOperationTask(String siteCode, String operationTaskNum) {
    String message = null;
    OperationTask operationTask = this.getOperationTask(siteCode, operationTaskNum);
    if (null == operationTask || StringUtils.isBlank(operationTask.getStatusECode())
        || !operationTask.getStatusECode().equals(OperationTaskStatus.CREATE.getValue())) {
      message = "OperationTaskStatusNotCreate.M";
    } else {
      operationTaskDao.updateOperationTaskStatus(siteCode, operationTaskNum, OperationTaskStatus.PLACE_ORDER.getValue(),
          new Date(), SecurityContextUtil.getCurrentUserName());
    }
    return message;
  }

  @Override
  public String revocationOperationTask(String siteCode, String operationTaskNum) {
    String message = null;
    OperationTask operationTask = this.getOperationTask(siteCode, operationTaskNum);
    if (null == operationTask || StringUtils.isBlank(operationTask.getStatusECode())
        || !operationTask.getStatusECode().equals(OperationTaskStatus.PLACE_ORDER.getValue())) {
      message = "OperationTaskStatusNotRelease.M";
    } else if (operationTaskConfirmService.whetheroperationTaskConfirm(siteCode, operationTaskNum)) {
      message = "OperationTaskStatusAlreadyConfirm.M";
    } else {
      operationTaskDao.updateOperationTaskStatus(siteCode, operationTaskNum, OperationTaskStatus.CREATE.getValue(),
          new Date(), SecurityContextUtil.getCurrentUserName());
    }
    return message;
  }

  @Override
  public String closeOperationTask(String siteCode, String operationTaskNum) {
    String message = null;
    OperationTask operationTask = this.getOperationTask(siteCode, operationTaskNum);
    if (null == operationTask || StringUtils.isBlank(operationTask.getStatusECode())
        || operationTask.getStatusECode().equals(OperationTaskStatus.CLOSE.getValue())) {
      message = "OperationTaskStatusIsClose.M";
    } else if (!operationTask.getStatusECode().equals(OperationTaskStatus.PLACE_ORDER.getValue())
        && !operationTask.getStatusECode().equals(OperationTaskStatus.CREATE.getValue())
        && !operationTask.getStatusECode().equals(OperationTaskStatus.INTO_PRODUCTION.getValue())) {
      message = "OperationTaskStatusNotClose.M";
    } else {
      operationTaskDao.updateOperationTaskStatus(siteCode, operationTaskNum, OperationTaskStatus.CLOSE.getValue(),
          new Date(), SecurityContextUtil.getCurrentUserName());
      //检查当前工序任务单上级生产管理号下所有工序任务单是否全部完工或关闭
      if (operationTaskDao.productionControlForOperationTaskCount(siteCode, operationTask.getProductionControlNum(),
          null, null) == operationTaskDao.productionControlForOperationTaskCount(siteCode,
              operationTask.getProductionControlNum(), OperationTaskStatus.CLOSE.getValue(),
              OperationTaskStatus.COMPLETE.getValue())) {
        productionControlService.doCloseProductionControl(siteCode, operationTask.getProductionControlNum(), false);
      }
    }
    return message;
  }

  @Override
  public List<OperationTask> getOperationTaskProgressByCondition(OperationTaskCondition condition) {
    if (condition == null) {
      return null;
    }
    return operationTaskDao.getOperationTaskProgressByCondition(condition);
  }

  @Override
  public int getOperationTaskProgressCountByCondition(OperationTaskCondition condition) {
    if (condition == null) {
      return 0;
    }
    return operationTaskDao.getOperationTaskProgressCountByCondition(condition);
  }

  @Override
  public List<OperationTask> getOperationArrangePlanByCondition(OperationTaskCondition condition) {
    if (condition == null) {
      return null;
    }
    return operationTaskDao.getOperationArrangePlanByCondition(condition);
  }

  @Override
  public int getOperationArrangePlanCountByCondition(OperationTaskCondition condition) {
    if (condition == null) {
      return 0;
    }
    return operationTaskDao.getOperationArrangePlanCountByCondition(condition);
  }

  @Override
  public void insertOperationTask(OperationTask operationTask) {
    operationTask.setOperationTaskNum(
        numberRuleService.selectNextNumber(operationTask.getSiteCode(), NumberRuleEnums.OPERATIONTASKNUM.getValue()));
    operationTaskDao.insetOperationTask(operationTask);
  }

  @Override
  public void updateOperationTask(OperationTask operationTask) {
    operationTaskDao.updateOperationTask(operationTask);
  }

  @Override
  public void deleteOperationTask(String siteCode, String operationTaskNum) {
    operationTaskDao.deleteOperationTask(siteCode, operationTaskNum);
  }

  @Override
  public void saveOperationTasks(List<OperationTask> tasks) {
    for (OperationTask task : tasks) {
      if (StringUtils.isEmpty(task.getOperationTaskNum())) {
        task.setOperationTaskNum(
            numberRuleService.selectNextNumber(task.getSiteCode(), NumberRuleEnums.OPERATIONTASKNUM.getValue()));
        operationTaskDao.insetOperationTask(task);
      } else {
        operationTaskDao.updateOperationTask(task);
      }
    }
  }

  @Override
  public Date getMinStartDateByOrderNums(String siteCode, String[] orderNums) {
    return operationTaskDao.getMinStartDateByOrderNums(siteCode, orderNums);
  }

  @Override
  public Date getMaxEndDateByOrderNums(String siteCode, String[] orderNums) {
    return operationTaskDao.getMaxEndDateByOrderNums(siteCode, orderNums);
  }

  @Override
  public List<OperationTask> getOperationTasksByControlNums(String siteCode, String[] controlNums) {
    return operationTaskDao.getOperationTasksByControlNums(siteCode, controlNums);
  }

  @Override
  public Set<String> getControlNumsByWorkUnitCodes(String siteCode, Set<String> workUnitCodeSet, String startDate,
      String endDate) {
    if (workUnitCodeSet.isEmpty()) {
      return new HashSet<String>();
    }
    return operationTaskDao.getControlNumsByWorkUnitCodes(siteCode, workUnitCodeSet, startDate, endDate);
  }

  @Override
  public void updateOperationTaskStatus(String siteCode, String operationTaskNum, String statusECode) {
    operationTaskDao.updateOperationTaskStatus(siteCode, operationTaskNum, statusECode, new Date(),
        SecurityContextUtil.getCurrentUserName());
  }

}
